package com.accompany.fragment.order.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.accompany.R;
import com.accompany.adapter.BaseRecyclerHolder;
import com.accompany.adapter.BaseRecyclerRefreshAdapter;
import com.accompany.api.bean.OrderComment;
import com.accompany.api.category.OrderCommCategory;
import com.accompany.api.presenter.OrderCommentPresenter;
import com.accompany.base.BaseActivity;
import com.accompany.dialog.LoginOutDialog;
import com.accompany.fragment.login.UserLoginActivity;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.utils.FullScreenUtils;
import com.accompany.utils.HttpNetworkConnectUtils;
import com.accompany.utils.SPUtils;
import com.accompany.utils.ToastUtil;
import com.accompany.view.RecyclerViewLayoutManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MineCommentsActivity extends BaseActivity implements OrderCommCategory.View {


    @BindView(R.id.base_back)
    ImageView baseBack;
    @BindView(R.id.base_title)
    TextView mTextPayMsg;
    @BindView(R.id.base_setting)
    ImageView baseSetting;
    @BindView(R.id.toolbar)
    RelativeLayout toolbar;
    @BindView(R.id.base_refresh_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.base_refresh)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.icn_image)
    ImageView icnImage;
    @BindView(R.id.relat_empty)
    RelativeLayout relatEmpty;


    private String mToken;
    private long acdId;
    private String orderId;
    private OrderCommentPresenter mPresenter;
    private LoginOutDialog dialog;
    private int pageNumber = 1;
    private boolean isLoading = false;
    private List<OrderComment.OrderCommentData.CommentList> mList = new ArrayList<>();
    private BaseRecyclerRefreshAdapter<OrderComment.OrderCommentData.CommentList> mAdapter;
    private RecyclerView mIconImageRecycler;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_comments;
    }

    @Override
    public void initLayoutView() {
        mTextPayMsg.setText("我的点评");
        acdId = getIntent().getLongExtra("acdId", 0);
        orderId = getIntent().getStringExtra("orderId");
        Log.e("orderId", orderId + "");
        mToken = (String) SPUtils.get(MineCommentsActivity.this, ConstantStaticUrl.mToken, "");
        FullScreenUtils.getInstance().fullScreen(MineCommentsActivity.this, true);

        mPresenter = new OrderCommentPresenter(MineCommentsActivity.this);
        SetPersenter(mPresenter);
        if (!HttpNetworkConnectUtils.isNetworkConnected(MineCommentsActivity.this)) {
            ToastUtil.showToastERROR(MineCommentsActivity.this, "网络不给力,请稍后重试");
        } else {
            mPresenter.getOrderComment(mToken, orderId,acdId);
        }


        RecyclerViewLayoutManager.setLinearVerticalItemDecorationManager(MineCommentsActivity.this, mRecyclerView);
        mAdapter = new BaseRecyclerRefreshAdapter<OrderComment.OrderCommentData.CommentList>(MineCommentsActivity.this, mList, R.layout.item_comment_view) {

            @Override
            public void convert(BaseRecyclerHolder holder, OrderComment.OrderCommentData.CommentList item, int position, boolean isScrolling) throws IOException {

                if (TextUtils.isEmpty(mList.get(position).getContent())){
                    holder.getView(R.id.content).setVisibility(View.GONE);
                }else {
                    holder.getView(R.id.content).setVisibility(View.VISIBLE);
                    holder.setText(R.id.content, mList.get(position).getContent());
                }
                holder.setText(R.id.rate, "服务评分: " + mList.get(position).getScore()+" 分");

                holder.setText(R.id.comment_date, mList.get(position).getCommentTime().split(" ")[0]);
                holder.setText(R.id.obl_msg, "陪诊——" + mList.get(position).getPzOrderDTO().getServiceName() );
                holder.setText(R.id.person, "就诊人: "+mList.get(position).getPzOrderDTO().getPatientName());
                holder.setText(R.id.hospital,  mList.get(position).getPzOrderDTO().getHospitalName() );
                holder.setText(R.id.office, mList.get(position).getPzOrderDTO().getOfficeName());

//
                holder.setText(R.id.service_time,  mList.get(position).getPzOrderDTO().getPeriodStr());
                mIconImageRecycler = holder.getView(R.id.recycl_image);
                if (mList.get(position).getMedias() != null && mList.get(position).getMedias().size() > 0) {

                    mIconImageRecycler.setVisibility(View.VISIBLE);
                    RecyclerViewLayoutManager.setGridLayoutManager(MineCommentsActivity.this, mIconImageRecycler, 3);
                    List<OrderComment.OrderCommentData.CommentList.Medias> imgs = mList.get(position).getMedias();
                    List<String> imgList = new ArrayList<>();
                    for (int i = 0; i < imgs.size(); i++) {
                        imgList.add(imgs.get(i).getUrl());
                    }
                    mIconImageRecycler.setAdapter(new BaseRecyclerRefreshAdapter<String>(MineCommentsActivity.this, imgList, R.layout.icon_image_view) {
                        @Override
                        public void convert(BaseRecyclerHolder holder, String item, int position, boolean isScrolling) throws IOException {
                            ImageView mIconImg = holder.getView(R.id.micon_img);
                            RequestOptions requestOptions = new RequestOptions();
                            Glide.with(MineCommentsActivity.this)
                                    .load(imgList.get(position))
                                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(15)))
                                    .into(mIconImg);
                            holder.getView(R.id.micon_img).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    //点击弹出dialog  查看图片

                                }
                            });
                        }
                    });
                } else {
                    mIconImageRecycler.setVisibility(View.GONE);
                }

            }
        };
        mAdapter.setDatas(mList);
        mRecyclerView.setAdapter(mAdapter);
    }

    @OnClick({R.id.base_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;

        }
    }


    @Override
    public void getOrderComment(OrderComment baseBean) {//绑定数据
        if (baseBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (baseBean.getData() != null && baseBean.getData().getList().size() > 0) {
                if (pageNumber == 1) {
                    if (isLoading) {
                        isLoading = false;
                    } else {
                        mList.clear();
                    }
                    mList.addAll(baseBean.getData().getList());
                    mAdapter.setDatas(baseBean.getData().getList());
                } else {
                    mList.addAll(mList.size(), baseBean.getData().getList());
                    mAdapter.add(baseBean.getData().getList());
                }
                relatEmpty.setVisibility(View.GONE);
                mRecyclerView.setVisibility(View.VISIBLE);
            } else {
                if (isLoading) {
                    isLoading = false;
                } else {
                    relatEmpty.setVisibility(View.VISIBLE);
                    mRecyclerView.setVisibility(View.GONE);
                }
            }
        } else if (baseBean.getCode() == ConstantStaticUrl.NO_LOGIN_CODE) {

            if (dialog == null) {
                dialog = LoginOutDialog.getLoginOutDialog(MineCommentsActivity.this);
            }
            if (dialog != null && !dialog.isShowing()) {
                dialog.show();
            }
            dialog.findViewById(R.id.circler_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.findViewById(R.id.to_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityWitchAnimation(UserLoginActivity.class);
                    SPUtils.clear(MineCommentsActivity.this);
                    finish();
                    dialog.dismiss();
                }
            });
        } else {
            ToastUtil.showToastINFO(MineCommentsActivity.this, baseBean.getMsg());
        }
    }

    @Override
    public void SetPersenter(Object persenter) {

    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(MineCommentsActivity.this, mErrorMsg);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}